using System;
using System.Text;
using System.Xml;
using System.Collections;
using SharpObjects.Configuration;
using SharpObjects.Configuration.ConfigurationProviders;
using SharpObjects.Logging;
using SharpObjects.Logging.Utils;

namespace SharpObjects.Logging.Formatters
{
	public class SingleLineLogFormatter : LogFormatterBase
	{
		new public abstract class Tags : LogFormatterBase.Tags
		{
			public const string timeFormat = "time_format";
		}

		protected string timeFormat = "G";

		public SingleLineLogFormatter()
		{
			this.MessageSeparator = "### ";
			this.ValueDelimiter = " | ";
		}


		//#region ILogFormatter Members


		public override string ApplyFormat(ILogEntry logEntry)
		{
			StringBuilder sb = new StringBuilder();

			sb.Append(this.MessageSeparator);
			sb.Append(DateTime.Now.ToString(this.timeFormat));
			sb.Append(this.ValueDelimiter);
			sb.Append("Category: ");
			sb.Append(logEntry.Category);
			sb.Append(this.ValueDelimiter);
			sb.Append("Origin: ");
			sb.Append(logEntry.Origin);
			sb.Append(this.ValueDelimiter);
			sb.Append("Machine Name: ");
			sb.Append(logEntry.MachineName);
			sb.Append(this.ValueDelimiter);

			if(ProcessInformation.UseProcessInfo)
			{
				sb.Append("Process Name: ");
				sb.Append(logEntry.ProcessName);
				sb.Append(this.ValueDelimiter);
				sb.Append("Process ID: ");
				sb.Append(logEntry.ProcessId.ToString());
				sb.Append(this.ValueDelimiter);
			}

			sb.Append("Thread ID: ");
			sb.Append(logEntry.ThreadId);
			sb.Append(this.ValueDelimiter);
			sb.Append("Thread Name: ");
			sb.Append(logEntry.ThreadName);
			sb.Append(this.ValueDelimiter);

			sb.Append("Message: ");
			sb.Append(logEntry.Message);

			if(logEntry.StackTrace != null && logEntry.StackTrace != string.Empty)
			{
				sb.Append(this.ValueDelimiter);
				sb.Append("Stack Trace: ");
				sb.Append(logEntry.StackTrace);
			}

			sb.Append(this.ValueDelimiter);
			sb.Append("\r\n");

			return sb.ToString();
		}


		//#endregion

		public override void Initialize(XmlElement initializationElement)
		{
			base.Initialize (initializationElement);

			IConfigurationReader config = new XmlConfigurationProvider(initializationElement);
			this.timeFormat = config.GetString(Tags.timeFormat, this.timeFormat);

		}

	}
}
